<?php

class admin_model_system_app extends model
{
    
    private $app_tables = null;
    private $api = 'http://www.mrbone.org/api/http/';
    
    public function get_apps()
    {
        $apps = array();
        $dir = dir(BONE_ADMIN . DS . 'apps');
        while (($file = $dir->read()) !== false)
        {
            if (is_dir(BONE_ADMIN . DS . 'apps' . DS . $file))
            {
                if (file_exists(BONE_ADMIN . DS . 'apps' . DS . $file . DS . $file . '.php'))
                {
                    $apps[] = bone::get_app($file);
                }
            }
        }
        $dir->close();
        
        return $apps;
    }
    
    // 获取应用的数据库安装情况
    public function get_db_info($app)
    {
        $info = new stdClass();
        $info->total = 0;
        $info->created = 0;
        $info->tables = array();
        
        $sql_file = BONE_ADMIN . DS . 'apps' . DS . $app . DS . 'install.sql';
        if(file_exists($sql_file))
        {
            $sql = strtolower( file_get_contents($sql_file) );
            
            $tables = array();
            
        	$pos = strpos($sql, 'create table');
        	while($pos!==false)
        	{
        		$start = strpos($sql, '`', $pos)+1;
        		$end = strpos($sql, '`', $start);
        		$tables[] = substr($sql, $start, $end-$start);
        		
        		$pos = strpos($sql, 'create table', $pos+1);
        	}
        	
        	$total = count($tables);
        	
        	if($total>0)
        	{
            	$info->total = $total;
            	
            	if($this->app_tables===null)
            	{
            	    $db = bone::get_db();
                    $this->app_tables = $db->get_results('SHOW TABLES');
            	}
        
            	$created = 0;
            	foreach($tables as $table)
            	{
            	    if(in_array($table, $this->app_tables))
            	    {
            	        $info->tables[$table] = true;
            	        $created++;
            	    }
            	    else
            	    {
            	        $info->tables[$table] = false;
            	    }
            	}
            	$info->created = $created;
        	}
        }
        
        return $info;
    }
    
    public function get_remote_apps()
    {
        $lib_http = bone::get_lib('http');
        $response = $lib_http->post($this->api . '/get_apps', post());
        
        $apps = json_decode($response);
        return $apps;
    }
    
    
    public function get_remote_app($app_id)
    {
        $lib_http = bone::get_lib('http');
        $response = $lib_http->get($this->api . '/get_app?app_id=' . $app_id);
        
        $app = json_decode($response);
    }
    
    
    // 安装应用文件
    public function install($app_id)
    {
        $lib_http = bone::get_lib('http');
        $response = $lib_http->get($this->api . '/get_app?app_id=' . $app_id);
        
        $app = json_decode($response);
    }
    

    // 删除应用
    public function uninstall($app)
    {
        $file = BONE_ADMIN . DS . 'apps' . DS . $app . DS . 'uninstall.sql';
        if (file_exists($file))
        {
            $db = bone::get_db();
            $sqls = $this->split_sql(file_get_contents($file));
            foreach ($sqls as $sql)
            {
                $db->execute($sql);
            }
        }
        
        $fso = bone::get_lib('fso');
        $fso->rm_dir(BONE_ROOT . DS . 'apps' . DS . $app);
        $fso->rm_dir(BONE_ADMIN . DS . 'apps' . DS . $app);
        
        return true;
    }
    
    
    

    private function split_sql($sql)
    {
        $start = 0;
        $open = false;
        $open_char = '';
        $end = strlen($sql);
        $sqls = array();
        for ($i = 0; $i < $end; $i++)
        {
            $current = substr($sql, $i, 1);
            if (($current == '"' || $current == '\''))
            {
                $n = 2;
                while (substr($sql, $i - $n + 1, 1) == '\\' && $n < $i)
                {
                    $n++;
                }
                if ($n % 2 == 0)
                {
                    if ($open)
                    {
                        if ($current == $open_char)
                        {
                            $open = false;
                            $open_char = '';
                        }
                    }
                    else
                    {
                        $open = true;
                        $open_char = $current;
                    }
                }
            }
            if (($current == ';' && !$open) || $i == $end - 1)
            {
                $sqls[] = substr($sql, $start, ($i - $start + 1));
                $start = $i + 1;
            }
        }
        
        return $sqls;
    }
}
?>